<?php

namespace app\common\model;

use think\Model;


class Enterprise extends Model
{

    

    

    // 表名
    protected $name = 'enterprise';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    /**
     * 分页列表
     */
    public static function getList($postData)
    {
        $where = '1=1 and a.partners is not null and a.employees is not null ';
        $order = 'a.id desc';
        $page = isset($postData['page']) && !empty($postData['page']) ? intval($postData['page']) : 1;
        $limit = isset($postData['limit']) && !empty($postData['limit']) ? intval($postData['limit']) : 10;
        $start = ($page - 1) * $limit;
        $field = "a.*";

        if(isset($postData['credit_no']) && !empty($postData['credit_no']))
        {
            $where .= " and a.credit_no like '%{$postData['credit_no']}%'";
        }
        if(isset($postData['name']) && !empty($postData['name']))
        {
            $where .= " and a.name like '%{$postData['name']}%'";
        }

        $list = db('enterprise')
            ->alias('a')
            ->where($where)
            ->order($order)
            ->limit($start, $limit)
            ->field($field)
            ->select();
        if (!empty($list)) {
            foreach ($list as &$v) {
                $v['createtimeText'] = isset($v['createtime']) && !empty($v['createtime']) ? date('Y-m-d H:i:s', $v['createtime']) : '';
                $v['employees'] = !empty($v['employees']) ? json_decode($v['employees']) : $v['employees'];
                $v['brances'] = !empty($v['brances']) ? json_decode($v['brances']) : $v['brances'];
                $v['changerecords'] = !empty($v['changerecords']) ? json_decode($v['changerecords']) : $v['changerecords'];
                $v['partners'] = !empty($v['partners']) ? json_decode($v['partners']) : $v['partners'];
                $v['abnormal'] = !empty($v['abnormal']) ? json_decode($v['abnormal']) : $v['abnormal'];
            }
            unset($v);
        }

        $total = db('enterprise')
            ->alias('a')
            ->where($where)
            ->count();

        return [
            'list' => $list,
            'total' => $total,
        ];

    }

    /**
     * 详情
     */
    public static function getInfo($where)
    {
        $info = db('enterprise')->where($where)->find();
        $info['employees'] = !empty($info['employees']) ? json_decode($info['employees'], true) : $info['employees'];
        $info['brances'] = !empty($info['brances']) ? json_decode($info['brances']) : $info['brances'];
        $info['changerecords'] = !empty($info['changerecords']) ? json_decode($info['changerecords']) : $info['changerecords'];
        $info['partners'] = !empty($info['partners']) ? json_decode($info['partners']) : $info['partners'];
        $info['abnormal'] = !empty($info['abnormal']) ? json_decode($info['abnormal']) : $info['abnormal'];
        return $info;
    }







}
